package com.mfin.bom.adapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mfin.bom.interfaces.IDocCharge;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.ILoanMaster;
import com.mfin.bom.interfaces.ILoanPreference;
import com.mfin.bom.objects.DocCharge;
import com.mfin.bom.objects.LoanPreference;
import com.mfin.vo.ObjectKey;

public class LoanPreferenceAdapter extends IIdentifiableAdapter implements
		ILoanPreference {

	LoanPreference wrapLoanPreference = null;

	public LoanPreferenceAdapter(LoanPreference wrapLoanPreference) {
		this.wrapLoanPreference = wrapLoanPreference;
		ObjectKey[] keys = new ObjectKey[3];
		keys[0] = new ObjectKey(wrapLoanPreference.getFinance().getId());
		keys[1] = new ObjectKey(wrapLoanPreference.getLoanMaster()
				.getLoanType());
		keys[2] = new ObjectKey(wrapLoanPreference.getId());
		setKeys(keys);
	}

	public LoanPreference getLoanPreference() {
		return wrapLoanPreference;
	}

	@Override
	public IFinance getFinance() {
		return new FinanceAdapter(wrapLoanPreference.getFinance());
	}

	@Override
	public void setFinance(IFinance bomFinance) {
		if (bomFinance != null) {
			FinanceAdapter financeAdapter = (FinanceAdapter) bomFinance;
			wrapLoanPreference.setFinance(financeAdapter.getFinance());
		}
	}

	@Override
	public ILoanMaster getLoanMaster() {
		return new LoanMasterAdapter(wrapLoanPreference.getLoanMaster());
	}

	@Override
	public void setLoanMaster(ILoanMaster bomLoanMaster) {
		if (bomLoanMaster != null) {
			LoanMasterAdapter loanmasterAdapter = (LoanMasterAdapter) bomLoanMaster;
			wrapLoanPreference.setLoanMaster(loanmasterAdapter.getLoanMaster());
		}
	}

	@Override
	public Double getInterestRate() {
		return wrapLoanPreference.getInterestRate();
	}

	@Override
	public void setInterestRate(Double interestRate) {
		wrapLoanPreference.setInterestRate(interestRate);
	}

	@Override
	public Integer getDuration() {
		return wrapLoanPreference.getDuration();
	}

	@Override
	public void setDuration(Integer duration) {
		wrapLoanPreference.setDuration(duration);
	}

	@Override
	public List<IDocCharge> getDocCharges() {
		Set<DocCharge> docCharges = wrapLoanPreference.getDocCharges();
		List<IDocCharge> bomDocCharges = new ArrayList<IDocCharge>();

		if (docCharges != null) {
			for (DocCharge docCharge : docCharges) {
				bomDocCharges.add(new DocChargeAdapter(docCharge));
			}
		}
		return bomDocCharges;
	}

	@Override
	public void setDocCharges(List<IDocCharge> bomDocCharges) {
		Set<DocCharge> docCharges = new HashSet<DocCharge>();
		if (bomDocCharges != null) {
			for (IDocCharge bomDocCharge : bomDocCharges) {
				docCharges
						.add(((DocChargeAdapter) bomDocCharge).getDocCharge());
			}
		}
		wrapLoanPreference.setDocCharges(docCharges);
	}
}
